package com.eedi.framework.permission.controller.org;

import com.eedi.framework.permission.controller.org.vo.role.*;
import com.eedi.framework.common.enums.CommonStatusEnum;
import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.permission.controller.org.vo.role.*;
import com.eedi.framework.permission.dal.dataobject.OrgRoleDO;
import com.eedi.framework.permission.enums.OrgRoleCodeEnum;
import com.eedi.framework.permission.service.OrgPermissionService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.Comparator;
import java.util.List;

import static com.eedi.framework.common.pojo.CommonResult.success;
import static java.util.Collections.singleton;

/**
 * @author liuyf
 */
@Tag(name = "经营界面 - 角色管理")
@RestController
@RequestMapping("/org-role")
@Validated
public class OrgRoleController {

    @Resource
    private OrgPermissionService orgPermissionService;

    @PostMapping("/create")
    @Operation(summary = "创建角色(org:role:create)")
    @PreAuthorize("@org.hasPermission('org:role:create')")
    public CommonResult<String> createRole(@Valid @RequestBody OrgRoleSaveReq createReqVO) {
        return success(orgPermissionService.createRole(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "修改角色(org:role:update)")
    @PreAuthorize("@org.hasPermission('org:role:update')")
    public CommonResult<Boolean> updateRole(@Valid @RequestBody OrgRoleUpdateReq updateReqVO) {
        orgPermissionService.updateRole(updateReqVO);
        return success(true);
    }

    @PutMapping("/update-status")
    @Operation(summary = "修改角色状态(org:role:update)")
    @PreAuthorize("@org.hasPermission('org:role:update')")
    public CommonResult<Boolean> updateRoleStatus(@Valid @RequestBody OrgRoleUpdateStatusReq reqVO) {
        orgPermissionService.updateRoleStatus(reqVO.getOrgRoleId(), reqVO.getOrgRoleStatus());
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除角色(org:role:delete)")
    @Parameter(name = "orgRoleId", description = "角色编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:role:delete')")
    public CommonResult<Boolean> deleteRole(@RequestParam("orgRoleId") String orgRoleId) {
        orgPermissionService.deleteRole(orgRoleId);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得角色信息(org:role:list)")
    @PreAuthorize("@org.hasPermission('org:role:list')")
    public CommonResult<OrgRoleResp> getRole(@RequestParam("orgRoleId") String orgRoleId) {
        OrgRoleDO role = orgPermissionService.getRole(orgRoleId);
        return success(BeanUtils.toBean(role, OrgRoleResp.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得角色分页(org:role:list)")
    @PreAuthorize("@org.hasPermission('org:role:list')")
    public CommonResult<PageResult<OrgRoleResp>> getRolePage(OrgRolePageReq pageReqVO) {
        pageReqVO.setOrgRoleCode(OrgRoleCodeEnum.DEFAULT.getCode());
        PageResult<OrgRoleDO> pageResult = orgPermissionService.getRolePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, OrgRoleResp.class));
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取角色精简信息列表", description = "只包含被开启的角色，主要用于前端的下拉选项")
    public CommonResult<List<OrgRoleSimpleResp>> getSimpleRoleList() {
        List<OrgRoleDO> list = orgPermissionService.getRoleListByStatus(singleton(CommonStatusEnum.ENABLE));
        list.sort(Comparator.comparing(OrgRoleDO::getOrgRoleSort));
        List<OrgRoleDO> filterList = list.stream().filter(x -> !OrgRoleCodeEnum.ORG_ADMIN.getCode().equals(x.getOrgRoleCode())).toList();
        return success(BeanUtils.toBean(filterList, OrgRoleSimpleResp.class));
    }

    @GetMapping("/get-roles")
    @Operation(summary = "根据机构用户ID获取角色列表(org:role:list)")
    @Parameter(name = "orgUserId", description = "用户编号", required = true, example = "1024")
    @PreAuthorize("@org.hasPermission('org:role:list')")
    public CommonResult<List<OrgRoleSimpleResp> > getRoles(String orgUserId) {
        return success(orgPermissionService.getByOrgUserId(orgUserId));
    }


}
